package com.mcptt.config.body.info.mcvideo;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;

import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.body.info.InfoBody_NewCall;
import com.mcptt.config.body.info.mcptt.McpttInfoBody;

import android.util.Xml;

public class McvideoInfoBody_NewCall extends InfoBody_NewCall {

	private static final String THIS_FILE = "McvideoInfoBody_NewCall";
//	private static final String End_Mcptt_Tag = "</mcpttinfo>";
	private static final String End_Mcvideo_Tag = "</mcvideoinfo>";

	private static final String Name_CallType = "session-type";
	private static final String Name_RequestUri = "mcvideo-request-uri";
	private static final String Name_CallingUserId = "mcvideo-calling-user-id";
	private static final String Name_CallingGroupId = "mcvideo-calling-group-id";
	private static final String Name_Url = "mcvideoURI";

	private McvideoInfoBody_NewCall() throws Exception
	{
	}

	public static McvideoInfoBody_NewCall parseXmlStr(String xmlStr) throws Exception 
	{    
		McvideoInfoBody_NewCall body = new McvideoInfoBody_NewCall();
		int valType = 0;
//		String key = End_Mcvideo_Tag;
		
	    int index = xmlStr.indexOf(End_Mcvideo_Tag);
	    Log.d(THIS_FILE, "McvideoInfoBody_NewCall parseXmlStr index = " + index);
	    
//	    if(index <= 0)
//	    {
//	    	key = End_Mcptt_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
	    xmlStr= xmlStr.substring(0, index + End_Mcvideo_Tag.length());
	    xmlStr = xmlStr.trim();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据        
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

			case XmlPullParser.START_DOCUMENT:         
				break;    
			case XmlPullParser.START_TAG:
				if (Name_CallType.equals(pullParser.getName()))
				{
					String value = pullParser.nextText();
					int callType = getCallTypeFromDesc(value);
					body.setCallType(callType);
				}
				else if (Name_RequestUri.equals(pullParser.getName()))
				{
					valType = 1;
				}
				else if (Name_CallingUserId.equals(pullParser.getName()))
				{
					valType = 2;
				}   
				else if (Name_CallingGroupId.equals(pullParser.getName()))
				{
					valType = 3;
				}  
				else if (Name_Url.equals(pullParser.getName()))
				{
					String value = pullParser.nextText();
//					Log.d(THIS_FILE, "McvideoInfoBody_NewCall parseXmlStr valType = " + valType + " value = " + value);
					if(valType == 1)
					{
						body.setRequestUri(value); 
					}
					else if(valType == 2)
					{
						body.setCallingUserId(value);
					}
					else if(valType == 3)
					{
						body.setCallingGroupId(value);
					}
					valType = 0;
				}  
				break;

			case XmlPullParser.END_TAG:
				break;

			}

			event = pullParser.next();
		}

		return body;
	}
	public boolean hasVideo(){ return true; }
	public boolean isEmergency()
	{
		return false;
	}
	public int getCallType()
	{
		return callType;
	}

	public void setCallType(int callType)
	{
		this.callType = callType;
	}

	public String getRequestUri()
	{
		return requestUri;
	}

	public void setRequestUri(String requestUri)
	{
		this.requestUri = requestUri;
	}

	public String getCallingUserId()
	{
		return callingUserId;
	}

	public void setCallingUserId(String callingUserId)
	{
		this.callingUserId = callingUserId;
	}

	public String getCallingGroupId()
	{
		return callingGroupId;
	}

	public void setCallingGroupId(String callingGroupId)
	{
		this.callingGroupId = callingGroupId;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("McpttInfoBody_NewCall  ");
		strBuf.append("callType=").append(callType).append(",");
		strBuf.append("requestUri=").append(requestUri).append(",");
		strBuf.append("callingUserId=").append(callingUserId).append(",");
		strBuf.append("callingGroupId=").append(callingGroupId).append(";");
		
		return strBuf.toString();
	}

	public String toXmlString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<mcpttinfo xmlns:mgktp=\"urn:3gpp:ns:mcpttGKTP:1.0\" xmlns=\"urn:3gpp:ns:mcpttInfo:1.0\">");
		strBuff.append("<mcptt-Params>");
		strBuff.append("<session-type>").append(getCallTypeDesc(callType)).append("</session-type>");

		strBuff.append("<mcptt-request-uri>");
		strBuff.append("<mcpttURI>").append(requestUri).append("</mcpttURI>");
		strBuff.append("</mcptt-request-uri>");

		strBuff.append("<mcptt-calling-user-id>");
		strBuff.append("<mcpttURI>").append(callingUserId).append("</mcpttURI>");
		strBuff.append("</mcptt-calling-user-id>");

		strBuff.append("<mcptt-calling-group-id>");
		strBuff.append("<mcpttURI>").append(callingGroupId).append("</mcpttURI>");
		strBuff.append("</mcptt-calling-group-id>");

		strBuff.append("<mcptt-calling-user-alias>test</mcptt-calling-user-alias>");
		strBuff.append("<mcptt-calling-group-alias>grp1</mcptt-calling-group-alias>");

		strBuff.append("</mcptt-Params>");
		strBuff.append("</mcpttinfo>   ");

		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}


	public static String getTestStr()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<mcpttinfo xmlns:mgktp=\"urn:3gpp:ns:mcpttGKTP:1.0\" xmlns=\"urn:3gpp:ns:mcpttInfo:1.0\">");
		strBuff.append("<mcptt-Params>");
		strBuff.append("<session-type>").append(getCallTypeDesc(CallAttr.CallType_Prearranged)).append("</session-type>");

		strBuff.append("<mcptt-request-uri>");
		strBuff.append("<mcpttURI>").append("sip:6009755001@mcptt.com").append("</mcpttURI>");
		strBuff.append("</mcptt-request-uri>");

		strBuff.append("<mcptt-calling-user-id>");
		strBuff.append("<mcpttURI>").append("sip:6009755003@mcptt.com").append("</mcpttURI>");
		strBuff.append("</mcptt-calling-user-id>");

		strBuff.append("<mcptt-calling-group-id>");
		strBuff.append("<mcpttURI>").append("sip:60097551@group.com").append("</mcpttURI>");
		strBuff.append("</mcptt-calling-group-id>");

		strBuff.append("<mcptt-calling-user-alias>test</mcptt-calling-user-alias>");
		strBuff.append("<mcptt-calling-group-alias>grp1</mcptt-calling-group-alias>");

		strBuff.append("</mcptt-Params>");
		strBuff.append("</mcpttinfo>   ");

		return strBuff.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
